import json


def split_and_dedup_by_level(input_file):
    """
    按level分成两个文件，并分别去重
    """
    try:
        with open(input_file, "r", encoding="utf-8") as f:
            data = json.load(f)

        merged_array = []

        # 合并所有数组
        for key, value in data.items():
            if isinstance(value, list):
                merged_array.extend(value)

        print(f"合并完成，原始数据: {len(merged_array)} 条")

        # 按级别分类
        level2_data = []  # 城市级别
        level3_data = []  # 区县级别
        other_data = []  # 其他级别

        for item in merged_array:
            if isinstance(item, dict):
                level = item.get("level", "")
                if level == "2":
                    level2_data.append(item)
                elif level == "3":
                    level3_data.append(item)
                else:
                    other_data.append(item)

        print(f"城市级别(level=2): {len(level2_data)} 条")
        print(f"区县级别(level=3): {len(level3_data)} 条")
        print(f"其他级别: {len(other_data)} 条")

        # 处理城市级别数据去重（根据cityId）
        if level2_data:
            seen_city_ids = set()
            unique_level2 = []

            for item in level2_data:
                city_id = item.get("cityId", "")
                if city_id and city_id not in seen_city_ids:
                    seen_city_ids.add(city_id)
                    unique_level2.append(item)

            with open("cities_level2.json", "w", encoding="utf-8") as f:
                json.dump(unique_level2, f, ensure_ascii=False, indent=2)

            print(f"城市级别去重: {len(level2_data)} -> {len(unique_level2)} 条")
            print("保存至: cities_level2.json")

        # 处理区县级别数据去重（根据cityId+districtId+district）
        if level3_data:
            seen_combinations = set()
            unique_level3 = []

            for item in level3_data:
                combination_key = (
                    item.get("cityId", ""),
                    item.get("districtId", ""),
                    item.get("district", ""),
                )

                if combination_key not in seen_combinations:
                    seen_combinations.add(combination_key)
                    unique_level3.append(item)

            with open("districts_level3.json", "w", encoding="utf-8") as f:
                json.dump(unique_level3, f, ensure_ascii=False, indent=2)

            print(f"区县级别去重: {len(level3_data)} -> {len(unique_level3)} 条")
            print("保存至: districts_level3.json")

        # 如果有其他级别数据，也保存一下
        if other_data:
            with open("others.json", "w", encoding="utf-8") as f:
                json.dump(other_data, f, ensure_ascii=False, indent=2)
            print(f"其他级别数据保存至: others.json ({len(other_data)} 条)")

    except Exception as e:
        print(f"错误: {e}")


if __name__ == "__main__":
    split_and_dedup_by_level("city.json")
